package cn.dgut.testEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.List;

/**
**定义事件的发布者
 **/

@Component
public class EventService {

    private List<String> blackList;

    @Autowired
    private ApplicationEventPublisher publisher;

    public void setBlackList(List<String> blackList) {
        this.blackList = blackList;
    }

    public void setPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    /**
     * 事件的发布
     */
    public void doPublish(String address,String content){
        if (blackList.contains(address)) {
            BlackListEvent event = new BlackListEvent(this, address, content);
            System.out.println("开始发布事件");
            publisher.publishEvent(event);
            System.out.println("事件发布完成");
        }
    }
}
